/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.Quests;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.QuestsRepository;
import me.zhengjie.service.QuestsService;
import me.zhengjie.service.dto.QuestsDto;
import me.zhengjie.service.dto.QuestsQueryCriteria;
import me.zhengjie.service.mapstruct.QuestsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wuhj
* @date 2023-09-16
**/
@Service
@RequiredArgsConstructor
public class QuestsServiceImpl implements QuestsService {

    private final QuestsRepository questsRepository;
    private final QuestsMapper questsMapper;

    @Override
    public PageResult<QuestsDto> queryAll(QuestsQueryCriteria criteria, Pageable pageable){
        Page<Quests> page = questsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(questsMapper::toDto));
    }

    @Override
    public List<QuestsDto> queryAll(QuestsQueryCriteria criteria){
        return questsMapper.toDto(questsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public QuestsDto findById(Long id) {
        Quests quests = questsRepository.findById(id).orElseGet(Quests::new);
        ValidationUtil.isNull(quests.getId(),"Quests","id",id);
        return questsMapper.toDto(quests);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Quests resources) {
        questsRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Quests resources) {
        Quests quests = questsRepository.findById(resources.getId()).orElseGet(Quests::new);
        ValidationUtil.isNull( quests.getId(),"Quests","id",resources.getId());
        quests.copy(resources);
        questsRepository.save(quests);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            questsRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<QuestsDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (QuestsDto quests : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("题目类型", quests.getType());
            map.put("内容", quests.getContent());
            map.put("创建者", quests.getCreateBy());
            map.put("更新者", quests.getUpdateBy());
            map.put("创建时间", quests.getCreateTime());
            map.put("更新时间", quests.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}